package logic

import (
	"context"
	"time"
	"xialou_platform/app/errorx"
	"xialou_platform/services/order/model"

	"xialou_platform/services/order/rpc/internal/svc"
	"xialou_platform/services/order/rpc/orderClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderUpdateLogic {
	return &OrderUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 更新订单
func (l *OrderUpdateLogic) OrderUpdate(in *orderClient.OrderParam) (*orderClient.EmptyBody, error) {
	err := l.svcCtx.OrderModel.Update(l.ctx, &model.Order{
		Id:            in.Id,
		Type:          in.Type,
		SourceId:      in.SourceId,
		Money:         in.Money,
		Uid:           in.Uid,
		Status:        in.Status,
		OrderNum:      in.OrderNum,
		TradeType:     in.TradeType,
		PayType:       in.PayType,
		TransactionId: in.TransactionId,
		OutRefundNo:   in.OutRefundNo,
		Remark:        in.Remark,
		CreateTime:    time.Unix(in.CreateTime, 10),
		UpdateTime:    time.Now(),
		DelFlag:       in.DelFlag,
	})
	if err != nil {
		return nil, errorx.NewReasonError("网络出小差了，请重试刷新再试。", err.Error())
	}
	return &orderClient.EmptyBody{}, nil
}
